function firstGet() {
    var startDate = $("#startDate").val();
    var endDate = $("#endDate").val();
    var gameId = $("#gameId>option:selected").data("sourceid");
    var categoryId = $("#categoryId>option:selected").data("categoryid");

    $.post("/manage/getData", {
        startDate: startDate,
        endDate: endDate,
        gameId: gameId,
        categoryId: categoryId,
        nextPage: 0,
        maxPage: 0
    }, function (data) {
        var json = $.parseJSON(data);
        var pages = json.ret.pages;
        var progressBar = $(".progress-bar");
        progressBar.width(0);
        progressBar.text("0%");
        query(0,pages,0);
    });
}

function query(nextPage,maxPage,add) {
    if(nextPage === maxPage){
        return;
    }
    var startDate = $("#startDate").val();
    var endDate = $("#endDate").val();
    var gameId = $("#gameId>option:selected").data("sourceid");
    var categoryId = $("#categoryId>option:selected").data("categoryid");
    var progressBar = $(".progress-bar");
    var progressWidth = 0;
    $.post("/manage/save-comment", {
        startDate: startDate,
        endDate: endDate,
        gameId: gameId,
        categoryId: categoryId,
        nextPage: nextPage,
        maxPage: maxPage
    }, function (json) {
        var data = $.parseJSON(json);
        add += data.add;
        $(".net-show").html("共导入"+add+"条数据");
        progressWidth = ((nextPage+1) / maxPage) * 100;
        progressBar.width(progressWidth  + "%");
        progressBar.text(parseInt(progressWidth) + "%");
        query(nextPage+1,maxPage,add);
    });
}

function datepicker() {
    var datepicker = $(".datepicker");
    datepicker.datepicker({
        changeMonth: true,
        changeYear: true
    });
    datepicker.change(function () {
        $(this).datepicker("option", "dateFormat", "yy-mm-dd");
    });
}

$(function () {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $("#comment-submit").click(function () {
        firstGet();
    });
    datepicker();
});
